#include <types.h>
#include <conio.h>
#include <schedule.h>

#include <sys/kmalloc.h>
#include <sys/task.h>
#include <sys/thread.h>
#include <sys/intr.h>
#include <sys/unistd.h>

static void irq_thread(void *arg);

/* irqs table of system */
static irq_t *irq_table[16];

/* called from hal code in irq.c */
void irq_run(int irqn)
{
    irq_t *irq;
    int rc;

    irq = irq_table[irqn];
    if (irq == NULL) {
        kprintf("random irq failed!\n");
        return;
    }

    assert(irq->isr != NULL);
    irq->cnt++;

    /* call isr */
    rc = (*irq->isr)(irq->data);
    if (rc == IRQ_HALF) {
        /* kick IST for irq */
        assert(irq->ist != NULL);
        irq->ist_req++;
	sched_wakeup(&irq->ist_event);
	assert(irq->ist_req != 0);
    }

    return;
}

